package com.guyu.common.core.enums;

import com.guyu.common.core.enums.able.IEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Locale;
import java.util.Optional;

/**
 * <p> 排序方式 </p>
 *
 * @date 2025/3/13
 */
@Getter
@AllArgsConstructor
public enum DirectionEnum implements IEnum<DirectionEnum> {

    ASC(1, "asc", "升序"),
    DESC(2, "desc", "降序");

    private final Integer code;
    private final String value;
    private final String desc;

    public static DirectionEnum fromString(String value) {

        try {
            return DirectionEnum.valueOf(value.toUpperCase(Locale.US));
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format(
                    "Invalid value '%s' for orders given; Has to be either 'desc' or 'asc' (case insensitive)", value), e);
        }
    }

    public static Optional<DirectionEnum> fromOptionalString(String value) {

        try {
            return Optional.of(fromString(value));
        } catch (IllegalArgumentException e) {
            return Optional.empty();
        }
    }

    public boolean isAscending() {
        return this.equals(ASC);
    }

    public boolean isDescending() {
        return this.equals(DESC);
    }
}
